angular.module('tipService', []).factory('tipService', ['$timeout', tipService]);

function tipService($timeout) {
    var service = {
        show: show,
        hide: hide
    };
    return service;

    function show(element, data, delay, cb) {
        delay = delay * 1000 || 3000;
        var position = "bottom";
        var notice = data.notice || "success";
        var message = data.message;
        //var tipsHtml = '<div class="tips-wrapper ' + position + '"><div class="tips"><i></i><span>' + data.message + '</span></div></div>';
        var tipsHtml = '<div class="alert alert-' + notice + ' dotips2">' + message + '</div>';

        var tips = angular.element(tipsHtml);

        if (element == 'body') {
            tips.appendTo("body").addClass("win");
        } else {
            var offset = element.offset();
            var top = position == "top" ? offset.top + (element.outerHeight() + angular.element(".tips").height() + 30) :
                offset.top - (element.outerHeight() + angular.element(".tips").height() + 30);
            tips.appendTo("body").css({
                top: top,
                left: offset.left
            });
        }
        $timeout(function() {
            hide(tips);
            if (cb && typeof cb === "function") {
                cb();
            }
        }, delay);
    }

    function hide(ele) {
        ele.remove();
    }

}
